package com.xbongbong.paas.log.model.impl;

import com.xbongbong.paas.log.MongoLogUtil;
import com.xbongbong.paas.log.MongoQueryConditions;
import com.xbongbong.paas.log.MongoQueryItem;
import com.xbongbong.paas.log.entity.OperateDetailLogEntity;
import com.xbongbong.paas.log.enums.FilterOperatorEnum;
import com.xbongbong.paas.log.model.OperateDetailLogModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 详细操作日志相关model方法实现
 * @author kaka
 * @date 2019/1/22 20:27
 * @since v1.0
 * @version v1.0
 */
@Service("operateDetailLogModel")
public class OperateDetailLogModelImpl implements OperateDetailLogModel {

    private static final Logger LOG = LoggerFactory.getLogger(OperateDetailLogModelImpl.class);

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 保存详细操作日志
     * @param entity 详细操作日志实体
     */
    @Override
    @Async(value = "sysLogThreadPool")
    public Integer insert(OperateDetailLogEntity entity) throws XbbException {
        try {
            mongoTemplate.save(entity);
        } catch (Exception e) {
            LOG.error("OperateDetailLogModelImpl.insert 执行出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return 1;
    }

    @Override
    public Integer update(OperateDetailLogEntity entity) throws XbbException {
        // 目前没有更新需求
        return null;
    }

    @Override
    public List findEntitys(Map<String, Object> param) {
        // 目前没有列表需求
        return null;
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        // 目前没有列表需求
        return null;
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public OperateDetailLogEntity getByOperateLogId(String corpid, String operateLogId) {
        MongoQueryConditions conditions = new MongoQueryConditions(BasicConstant.ONE);
        //corpid额外设置
        conditions.setCorpid(corpid);
        conditions.addCondition(new MongoQueryItem("operateLogId", FilterOperatorEnum.EQUAL, operateLogId));
        Query query = MongoLogUtil.formatQuery(conditions,false);
        OperateDetailLogEntity operateDetailLogEntity = mongoTemplate.findOne(query, OperateDetailLogEntity.class);
        return operateDetailLogEntity;
    }
}
